/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit;

import com.ssm.changeunit.util.FileUtil;
import com.ssm.changeunit.Environment;
import com.ssm.changeunit.persistance.DatabaseManager;
import com.ssm.changeunit.persistance.PersistanceException;
import com.ssm.changeunit.util.LogUtil;
import java.io.IOException;
import java.sql.*;
import junit.framework.TestCase;
import org.apache.log4j.Logger;

/**
 * ChangeUnit의 TestCase의 최상위 클래스
 * @author 시리우스
 */
public class ChangeUnitTestCase extends TestCase {
    private Logger log = Logger.getLogger(ChangeUnitTestCase.class);
    
    public ChangeUnitTestCase(String testName) {
        super(testName);
    }
    
    protected void deleteAllData() {
        log.info("모든 데이터를 삭제합니다.");
        
        String driver = "org.apache.derby.jdbc.EmbeddedDriver";  
    
        // ConnectionURL 
        String connectionURL = "jdbc:derby:" + com.ssm.changeunit.Environment.DATABASE_NAME + ";";  
    
        try {
            String databaseSql[] = {
                "delete from ManagedChangeUnit",
                "delete from project",
                "delete from TAG",
                "delete from manageditemlist",
                "delete from parent",
                "delete from child",
                "delete from changelog",
                "delete from changeunitbranch",
                "delete from branch",
                "delete from changeunititem",
                "delete from changeunit",
                "delete from USERS"
            };

            Connection conn = null;

            try { 
                // Database driver loading 
                Class.forName(driver);  

                conn = DriverManager.getConnection(connectionURL); 
            } catch(ClassNotFoundException ex) { 
                // Driver class를 찾지 못한경우 
                throw new PersistanceException("Database driver 클래스를 찾을 수 없습니다.", ex); 
            } catch(SQLException ex) { 
                // 커넥션을 얻지 못한 경우 
                throw new PersistanceException("Connection을 얻을수 없습니다.", ex); 
            } 

            // 검색 루틴 구현
            Statement stmt = null; 

            try {
                conn.setAutoCommit(true);   
                stmt = conn.createStatement(); 
                
                for(String subSql : databaseSql) 
                    stmt.executeUpdate(subSql);
            }
            catch(SQLException e) { 
                throw new PersistanceException("Exception while executing ChangeUnitTestCase.deleteAllData: "+ e, e);
            } 
            finally {
                if(stmt != null) 
                    try { stmt.close(); }
                    catch(Exception ignore) { }  
                if(conn != null) 
                    try { conn.close(); } 
                    catch(Exception ignore) { } 
            }
        }
        catch(PersistanceException ex) {
            log.fatal(LogUtil.stackTraceToString(ex));
        }
    }
    
    protected void printVersion(String changeUnitId) {
        try {
            ChangeUnit changeUnit = new ChangeUnitFactory().createProduct(changeUnitId);
            
            log.debug("ID: " + changeUnit.getChangeUnitId());
            log.debug("Author: " + changeUnit.getUserId());
            log.debug("Time: " + changeUnit.getTime());
            log.debug("Log: " + changeUnit.getLog());
            log.debug("Branch:");
            for(Branch branch : changeUnit.getBranchs()) 
                log.debug(branch.getBranchName());
            
            for(ChangeUnitItem changeUnitItem : changeUnit.getChangeUnitItems()) {
                log.debug("-----------------------------------------------------------");
                log.debug("ChangeUnitItem ID: " + changeUnitItem.getChangeUnitItemId());
                log.debug("ChangeUnitItem FilePath: " + changeUnitItem.getFilePath());
                log.debug("ChangeUnitItem RefChangeLog: " + changeUnitItem.getRefChangeLog().getChangeLogId());
                log.debug("ChangeUnitItem ");
                log.debug("\n" + new String(changeUnitItem.getRefChangeLog().getContent()));
                log.debug("-----------------------------------------------------------");
            }
        }
        catch(CreateException ex) {
            log.fatal(LogUtil.stackTraceToString(ex));
        }
        catch(PersistanceException ex) {
            log.fatal(LogUtil.stackTraceToString(ex));
        }
    }
}
